﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">账&emsp;号:</label>
                        <div class="layui-input-inline">
                            <input name="username" class="layui-input" placeholder="输入账号" />
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">状&emsp;态:</label>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="-1">选择状态</option>
                                <option value="1">正常</option>
                                <option value="0">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="userTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reset">重置密码</a>
</script>


<!-- 表格状态列 -->
<script type="text/html" id="userTbState">
    <input type="checkbox" lay-filter="userTbStateCk" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|禁用" {{d.status==1?'checked':''}} style="display: none;" />
    <p style="display: none;">{{d.status==1?'正常':'禁用'}}</p>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="userEditDialog">
    <form id="userEditForm" lay-filter="userEditForm" class="layui-form model-form">
        <input name="id" value="0" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">账号:</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">状态:</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常" checked />
                <input type="radio" name="status" value="0" title="禁用" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色:</label>
            <div class="layui-input-block">
                <div id="role"></div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="userEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<script>layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        /* 渲染表格 */
        var insTb = table.render({
            elem: '#userTable',
            url: '/api/admin/GetAdminList',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                { type: 'checkbox' },
                { field: 'id', width: 100 },
                { field: 'username', title: '账号', sort: true },
                {
                    field: 'roles', title: '角色', templet: function (d) {
                        return d.roles.map(function (item) {
                            return '<span class="layui-badge layui-badge-gray">' + item.rolename + '</span>';
                        }).join('&nbsp;&nbsp;');
                    }, sort: true, width: 150
                },
                {
                    field: 'createtime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createtime);
                    }, sort: true
                },
                {
                    field: 'lasttime', title: '最后时间', templet: function (d) {
                        return util.toDateString(d.lasttime);
                    }, sort: true
                },
                { field: 'lastip', title: 'IP' },
                { field: 'status', title: '状态', templet: '#userTbState', sort: true, width: 100 },
                { title: '操作', toolbar: '#userTbBar', align: 'center', minWidth: 200 }
            ]]
        });

        /* 表格搜索 */
        form.on('submit(userTbSearch)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } });
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(userTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(userTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('userTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ ids: ids });
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '用户',
                content: $('#userEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('userEditForm', mData);
                   
                    
                    
                    $.get("/api/admin/GetAllRole", {}, function (res) {
                        res = JSON.parse(res);
                        // 渲染多选下拉框
                        var insRoleSel = xmSelect.render({
                            el: '#role',
                            name: 'role',
                            layVerify: 'required',
                            layVerType: 'tips',
                            data: res
                        });
                        // 回显选中角色
                        if (mData && mData.roles) {
                            insRoleSel.setValue(mData.roles.map(function (item) {
                                return item.id;
                            }));
                        }
                        // 表单提交事件
                        form.on('submit(userEditSubmit)', function (data) {
                            data.field.roles = insRoleSel.getValue('valueStr');
                            var loadIndex = layer.load(2);
                            $.post(mData ? '/api/admin/EditAdminInfo' : '/api/admin/AddAdminInfo', data.field, function (res2) {
                                layer.close(loadIndex);
                                if (1 == res2.code) {
                                    layer.close(dIndex);
                                    layer.msg(res2.message, { icon: 1 });
                                    insTb.reload({ page: { curr: 1 } });
                                } else {
                                    layer.msg(res2.message, { icon: 2 });
                                }
                            }, 'json');
                            return false;
                        });
                        // 禁止弹窗出现滚动条
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    });
                    
                  
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                    $.post('/api/admin/DeleteAdminInfo', {
                        ids: obj.data ? obj.data.id : obj.ids.join(',')
                    }, function (res) {
                        layer.close(loadIndex);
                        if (res.code == 1) {
                            layer.msg(res.message, { icon: 1 });
                            insTb.reload({ page: { curr: 1 } });
                        } else {
                            layer.msg(res.message, { icon: 2 });
                        }
                    }, 'json');
            });
        }

        /* 修改用户状态 */
        form.on('switch(userTbStateCk)', function (obj) {
            var loadIndex = layer.load(2);
            $.post('/api/admin/EditAdminStatus', {
                id: obj.elem.value,
                status: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.close(loadIndex);
                if (res.code == 1) {
                    layer.msg(res.message, { icon: 1 });
                } else {
                    layer.msg(res.message, { icon: 2 });
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        /* 重置密码 */
    function resetPsw(obj) {
        parent.layui.admin.prompt({
            title: '请输入新“' + obj.data.username + '”的登录密码',
        }, function (value, index, elem) {
                $.post('/api/admin/UpdateAdminPassword', {
                    uid: obj.data.id, password: value
                }, function (res) {
                        parent.layer.close(index);
                        if (res.code == 1) {
                            layer.msg(res.message, { icon: 1 });
                    } else {
                            layer.msg(res.message, { icon: 2 });
                    }
                }, 'json');
        });
            //layer.confirm('确定要重置“' + obj.data.username + '”的登录密码吗？', {
            //    skin: 'layui-layer-admin',
            //    shade: .1
            //}, function (i) {
            //    layer.close(i);
            //    var loadIndex = layer.load(2);
            //    $.get('../../json/ok.json', {
            //        userId: obj.data.userId
            //    }, function (res) {
            //        layer.close(loadIndex);
            //        if (res.code === 200) {
            //            layer.msg(res.msg, { icon: 1 });
            //        } else {
            //            layer.msg(res.msg, { icon: 2 });
            //        }
            //    }, 'json');
            //});
        }

    });</script>